﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Genuilder.Tests
{
	public class TestDirectory : IDisposable
	{
		public TestDirectory()
		{
			_DirectoryInfo = new DirectoryInfo(Guid.NewGuid().ToString());
			_DirectoryInfo.Create();
		}
		private readonly DirectoryInfo _DirectoryInfo;
		public DirectoryInfo DirectoryInfo
		{
			get
			{
				return _DirectoryInfo;
			}
		}
		public TestDirectory Of(string directory)
		{
			var directoryInfo = new DirectoryInfo(directory);
			Copy(_DirectoryInfo, directoryInfo);
			return this;
		}

		private void Copy(DirectoryInfo dest, DirectoryInfo source)
		{
			foreach(var file in source.GetFiles())
			{
				file.CopyTo(Path.Combine(dest.FullName, file.Name), true);
			}
			foreach(var directory in source.GetDirectories())
			{
				var subDirectory = dest.CreateSubdirectory(directory.Name);
				Copy(subDirectory, directory);
			}
		}

		#region IDisposable Members

		public void Dispose()
		{
			_DirectoryInfo.Delete(true);
		}

		#endregion
	}
}
